<?php
class Site_Model_HasNews extends Promotor_Model_Abstract {
	
	protected $_dbTableClass = 'Site_Model_DbTable_HasNews';
	
	/**
	 * @param integer $newsId
	 * @param array $pages
	 */
	public function attachNewsToSites($newsId, array $sites)
	{
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		
		// filtrowanie przed zduplikowanymi kluczami!
		$sites = array_keys(array_flip($sites));

		$db->beginTransaction();
		$table->delete($db->quoteInto('news_id = ?', $newsId));
		try {
			foreach ($sites as $siteId)
			{
				if (!is_numeric($siteId))
				{
					continue;
				}
				$table->insert(array(
					'news_id' => $newsId,
					'site_id' => $siteId
				));
			}
			$db->commit();
			$this->_setStatus(self::SUCCESS);
		} catch (Zend_Db_Exception $e) {
			$db->rollBack();
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
}